package com.zq.assistant.utils;

import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.zq.assistant.app.App;

import java.math.BigDecimal;

/**
 * Created by zhangqiang on 17-5-26.
 */

public class AppUtils {

    public static String VERSION_NAME;

    public static int VERSION_CODE;

    static {

        try {
            PackageInfo packageInfo = App.instance.getPackageManager().getPackageInfo(App.instance.getPackageName(), 0);
            VERSION_NAME = packageInfo.versionName;
            VERSION_CODE = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static String getImagePathFromActivityResult(Context context, Intent data) {

        Uri uri = data.getData();

        Log.i("Test", "========uri=====" + uri.toString());
        String imagePath = null;
        if (DocumentsContract.isDocumentUri(context, uri)) {
            // 如果是document类型的Uri，则通过document id处理
            String docId = DocumentsContract.getDocumentId(uri);
            if ("com.android.providers.media.documents".equals(uri
                    .getAuthority())) {
                String id = docId.split(":")[1]; // 解析出数字格式的id
                String selection = MediaStore.Video.Media._ID + "=" + id;
                imagePath = getImagePath(context, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, selection);
            } else if ("com.android.providers.downloads.documents".equals(uri
                    .getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"),
                        Long.valueOf(docId));
                imagePath = getImagePath(context, contentUri, null);
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            // 如果不是document类型的Uri，则使用普通方式处理
            imagePath = getImagePath(context, uri, null);
        }
        return imagePath;
    }

    private static String getImagePath(Context context, Uri uri, String selection) {

        Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);

        if (cursor != null && cursor.moveToNext()) {

            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            cursor.close();
            return path;
        }
        return null;
    }

    public static String getFileTransferSpeedDisplay(long bytesPerSecond) {

        if (bytesPerSecond < 1024 * 1024) {

            return new BigDecimal(bytesPerSecond / 1024f).setScale(2, BigDecimal.ROUND_HALF_UP) + "KB/s";
        } else {

            return new BigDecimal(bytesPerSecond / (1024 * 1024f)).setScale(2, BigDecimal.ROUND_HALF_UP) + "MB/s";
        }
    }
}
